package org.jeecg.modules.wangyue.utils;

import com.alibaba.fastjson.JSONArray;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import org.jeecg.common.api.vo.Result;

import java.util.HashMap;
import java.util.Map;

public class SendSmsUtil {

    private static final String PRODUCT = "Dysmsapi"; //产品名称: 云通信短信API产品，（开发者无需替换）
    private static final String DOMAIN = "dysmsapi.aliyuncs.com"; //产品域名，（开发者无需更换）
//    private static final String ACCESS_KEYID = "LTAI5tB5GthVcKuGL5vFY4YH"; //开发者自己的AccessKeyID，（阿里云账号内可创建）
//    private static final String ACCESSKEY_SECRET = "D5KaxHHULpXVosQa0cxqMJV181Qf86"; //开发者自己的AccessKeySecret，（阿里云账号内可创建）
//    private static final String SIGN = "阿里云短信测试"; //短信签名，(可在短信服务签名中找到)
//    private static final String TEMPLATE = "SMS_154950909"; //短信模板，(可在短信服务模板中找到)
    private static final String ACCESS_KEYID = "LTAI5tHXoxQeedtQCTiHnz9o"; //开发者自己的AccessKeyID，（阿里云账号内可创建）
    private static final String ACCESSKEY_SECRET = "KyXjnhnd3bcALA9ffQQrHsUTUcYgkr"; //开发者自己的AccessKeySecret，（阿里云账号内可创建）
    private static final String SIGN = "阿里云短信测试"; //短信签名，(可在短信服务签名中找到)
    private static final String TEMPLATE = "SMS_154950909"; //短信模板，(可在短信服务模板中找到)


    /**
     * 发送短信验证码
     * @param phoneNumber   接收短信的手机号
     * @param code          验证码
     * @return
     * @throws Exception
     */
    public static SendSmsResponse sendSmsCode(String phoneNumber, String code) throws Exception {
        Config config = new Config()
                .setAccessKeyId(ACCESS_KEYID)
                .setAccessKeySecret(ACCESSKEY_SECRET);
        config.endpoint = DOMAIN; //访问的域名
        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(SIGN)
                .setTemplateCode(TEMPLATE)
                .setPhoneNumbers(phoneNumber)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
        return sendSmsResponse;
    }


    /**
     * 发送短信验证码(有返回信息)
     * @param phoneNumber   接收短信的手机号
     * @param code          验证码
     * @return
     */
    public static Result<?> sendSmsCodeResult(String phoneNumber, String code){
        try{
            SendSmsResponse sendSmsResponse = sendSmsCode(phoneNumber, code);
            String bodyCode = sendSmsResponse.getBody().getCode();
            if("OK".equals(bodyCode)){
                return Result.OK("短信发送成功");
            }else if("isv.AMOUNT_NOT_ENOUGH".equals(bodyCode)){
                return Result.error("短信功能余额不足");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error("短信发送失败");
    }



    public static void main(String[] args_) throws Exception {
        SendSmsResponse sendSmsResponse = SendSmsUtil.sendSmsCode("13434629414", CommonUtil.generateCode6());
        System.out.println(sendSmsResponse.getBody().getCode());
//        SendSmsResponse sendSmsResponse = SendSmsUtil.sendSms("18627219469", CommonUtil.generateCode6());
        if("OK".equals(sendSmsResponse.getBody().getCode())){
            System.out.println("短信发送成功");
        }else if("isv.AMOUNT_NOT_ENOUGH".equals(sendSmsResponse.getBody().getCode())){
            System.out.println("短信功能余额不足");
        }else{
            System.out.println("短信发送失败");
        }

    }

}
